﻿Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class Control_Raovat_AnToan
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            If Not Request.QueryString("RaovatId") Is Nothing Then
                hidID.Value = Request.QueryString("RaovatId")
                ShowData()
            End If
        End If


    End Sub

    Protected Sub ShowData()
        Using data As New VSEntities
            Dim p As AnToan = (From q In data.AnToans Where q.RaoVatId = hidRaoVatId.Value Select q).FirstOrDefault
            If Not p Is Nothing Then
                rptTuiKhiNguoiLai.Checked = IIf(IsNothing(p.TuiKhiNguoiLai) = True, 0, p.TuiKhiNguoiLai)
                rptTuiKhiTruoc.Checked = IIf(IsNothing(p.TuiKhiTruoc) = True, 0, p.TuiKhiTruoc)
                rptTuiKhisau.Checked = IIf(IsNothing(p.TuiKhiSau) = True, 0, p.TuiKhiSau)
                rptHaiBenGhe.Checked = IIf(IsNothing(p.TuiKhiHaiBenGhe) = True, 0, p.TuiKhiHaiBenGhe)
                rptABS.Checked = IIf(IsNothing(p.PhanhABS) = True, 0, p.PhanhABS)
                ' rpt.Checked = IIf(IsNothing(p.PhanBoLucABD) = True, 0, p.PhanBoLucABD)
                rptEBA.Checked = IIf(IsNothing(p.TroLucPhanhEBA) = True, 0, p.TroLucPhanhEBA)
                rptESB.Checked = IIf(IsNothing(p.CanBangDienTuESP) = True, 0, p.CanBangDienTuESP)
                rptDKHanhTrinh.Checked = IIf(IsNothing(p.DieuKhienHanhTrinh) = True, 0, p.DieuKhienHanhTrinh)
                rptCanhBaoLui.Checked = IIf(IsNothing(p.HoTroCanhBaoLui) = True, 0, p.HoTroCanhBaoLui)
                rptKiemSoatTruot.Checked = IIf(IsNothing(p.KiemSoatTruot) = True, 0, p.KiemSoatTruot)
                rptChotAntoan.Checked = IIf(IsNothing(p.ChotCuaAnToan) = True, 0, p.ChotCuaAnToan)
                rptKhoaCuaTuDong.Checked = IIf(IsNothing(p.KhoaCuaTuDong) = True, 0, p.KhoaCuaTuDong)
                rptKhoaCuaDKTuXa.Checked = IIf(IsNothing(p.KhoaCuaDKTuXa) = True, 0, p.KhoaCuaDKTuXa)
                rptKhoaDongCo.Checked = IIf(IsNothing(p.KhoaDongCo) = True, 0, p.KhoaDongCo)
                rptChongTrom.Checked = IIf(IsNothing(p.HTBaoNgoaiVi) = True, 0, p.HTBaoNgoaiVi)
                rptDenSuongMu.Checked = IIf(IsNothing(p.DenSuongMu) = True, 0, p.DenSuongMu)
                rptThatDayAnToan.Checked = IIf(IsNothing(p.DenDayAnToan) = True, 0, p.DenDayAnToan)
                rptDenPhu.Checked = IIf(IsNothing(p.DenPhanhPhu3) = True, 0, p.DenPhanhPhu3)

            End If
        End Using
    End Sub

#End Region
#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            If hidRaoVatId.Value <> "0" Then
                Dim p As AnToan = (From q In data.AnToans Where q.RaoVatId = hidID.Value Select q).FirstOrDefault

                Try
                    p.TuiKhiNguoiLai = rptTuiKhiNguoiLai.Checked
                    p.TuiKhiTruoc = rptTuiKhiTruoc.Checked
                    p.TuiKhiSau = rptTuiKhisau.Checked
                    p.TuiKhiHaiBenGhe = rptHaiBenGhe.Checked
                    p.PhanhABS = rptABS.Checked
                    p.PhanBoLucABD = rptPhanhDienTu.Checked
                    p.TroLucPhanhEBA = rptEBA.Checked
                    p.CanBangDienTuESP = rptESB.Checked
                    p.DieuKhienHanhTrinh = rptDKHanhTrinh.Checked
                    p.HoTroCanhBaoLui = rptCanhBaoLui.Checked
                    p.KiemSoatTruot = rptKiemSoatTruot.Checked
                    p.ChotCuaAnToan = rptChotAntoan.Checked
                    p.KhoaCuaTuDong = rptKhoaCuaTuDong.Checked
                    p.KhoaCuaDKTuXa = rptKhoaCuaDKTuXa.Checked
                    p.KhoaDongCo = rptKhoaDongCo.Checked
                    p.HTBaoNgoaiVi = rptChongTrom.Checked
                    p.DenSuongMu = rptDenSuongMu.Checked
                    p.DenDayAnToan = rptThatDayAnToan.Checked
                    p.DenPhanhPhu3 = rptDenPhu.Checked
                    p.RaoVatId = hidRaoVatId.Value

                    data.SaveChanges()
                    'Insert_App_Log("Insert Advertise:" & txtTitle.Text.Trim & "", Function_Name.Advertisement, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                    Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Raovat/Step3.aspx?RaovatId=" + hidRaoVatId.Value + "';", Me.Page, True)
                Catch ex As Exception
                    log4net.Config.XmlConfigurator.Configure()
                    log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                    Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
                End Try
            Else
                Dim p As New VSModel.AnToan
                Try
                    p.TuiKhiNguoiLai = rptTuiKhiNguoiLai.Checked
                    p.TuiKhiTruoc = rptTuiKhiTruoc.Checked
                    p.TuiKhiSau = rptTuiKhisau.Checked
                    p.TuiKhiHaiBenGhe = rptHaiBenGhe.Checked
                    p.PhanhABS = rptABS.Checked
                    p.PhanBoLucABD = rptPhanhDienTu.Checked
                    p.TroLucPhanhEBA = rptEBA.Checked
                    p.CanBangDienTuESP = rptESB.Checked
                    p.DieuKhienHanhTrinh = rptDKHanhTrinh.Checked
                    p.HoTroCanhBaoLui = rptCanhBaoLui.Checked
                    p.KiemSoatTruot = rptKiemSoatTruot.Checked
                    p.ChotCuaAnToan = rptChotAntoan.Checked
                    p.KhoaCuaTuDong = rptKhoaCuaTuDong.Checked
                    p.KhoaCuaDKTuXa = rptKhoaCuaDKTuXa.Checked
                    p.KhoaDongCo = rptKhoaDongCo.Checked
                    p.HTBaoNgoaiVi = rptChongTrom.Checked
                    p.DenSuongMu = rptDenSuongMu.Checked
                    p.DenDayAnToan = rptThatDayAnToan.Checked
                    p.DenPhanhPhu3 = rptDenPhu.Checked
                    p.RaoVatId = hidRaoVatId.Value
                    data.AnToans.AddObject(p)
                    data.SaveChanges()
                    'Insert_App_Log("Insert Advertise:" & txtTitle.Text.Trim & "", Function_Name.Advertisement, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                    Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Raovat/Step3.aspx?RaovatId=" + hidRaoVatId.Value + "';", Me.Page, True)
                Catch ex As Exception
                    log4net.Config.XmlConfigurator.Configure()
                    log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                    Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
                End Try
            End If
           
        End Using
    End Sub
    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region
End Class
